﻿using Keysight.Ccl.Wsl.UI;
using Keysight.Ccl.Wsl.UI.Managers;
using Keysight.Ccl.Wsl.UI.Resources.Skins;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace stringmanagekeysight
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : WslMainWindow
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void GenerateStringButton_Click(object sender, RoutedEventArgs e)
        {
            string s = "";

            string s1 = this.RichTextBox1.Text;
            string s2 = s1.Replace("\\", "\\\\");
            s2 = s2.Replace("\"", "\\\"");
            char[] split = new char[] { '\n' };
            string[] temp = s2.Split(split);

            if (this.ThemeComboBox.Text == "string_builder")
            {
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i].Trim().Length > 2)
                    {
                        if (!temp[i].Trim().Substring(0, 2).Equals("//"))
                        {
                            s += "strTrans.Append(\"" + temp[i] + "\\n\");\n";
                        }
                        else
                        {
                            s += temp[i] + "\n";
                        }
                    }
                    else
                    {
                        s += "strTrans.Append(\"" + temp[i] + "\\n\");\n";
                    }
                }
            }
            else if (this.ThemeComboBox.Text == "Command")
            {

                string strTrans = "";
                strTrans += "Command=\"{Binding NavigateCommand}\"\n";
                strTrans += "\n";

                strTrans += " public ICommand NavigateCommand\n";
                strTrans += " {\n";
                strTrans += "  get\n";
                strTrans += "  {\n";
                strTrans += "   return new DelegateCommand<TreeNodeConfig>(Navigate);\n";
                strTrans += "  }\n";
                strTrans += " }\n";
                strTrans += "  private void Navigate(TreeNodeConfig selectedItem)\n";
                strTrans += "  {\n";
                strTrans += "  }\n";

                s += strTrans;
            }
            
            else if (this.ThemeComboBox.Text == "属性")
            {

                string strTrans = "";
                strTrans += "  private bool _"+s1+";\n";
                strTrans += "  public bool "+s1+"\n";
                strTrans += "  {\n";
                strTrans += "      get { return _"+s1+"; }\n";
                strTrans += "      set\n";
                strTrans += "      {\n";
                strTrans += "          _"+s1+" = value;\n";
                strTrans += "          RaisePropertyChanged(nameof("+s1+"));\n";
                strTrans += "       }\n";
                strTrans += "  }\n";

                s += strTrans;
            }
            else if (this.ThemeComboBox.Text == "+")
            {
                s += "string strTrans=\"\";\n";
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i].Trim().Length > 2)
                    {
                        if (!temp[i].Trim().Substring(0, 2).Equals("//"))
                        {
                            s += "strTrans+=\"" + temp[i].Trim('\n').Trim('\r') + "\\n\";\n";
                        }
                        else
                        {
                            s += temp[i].Trim('\n').Trim('\r') + "\n";
                        }
                    }
                    else
                    {
                        s += "strTrans+=\"" + temp[i].Trim('\n').Trim('\r') + "\\n\";\n";
                    }
                }
            }
            else if (this.ThemeComboBox.Text == "MD5Hash")
            {
                s = MD5Hash(s2);
            }

            this.RichTextBox2.Text = s;

        }
        public static string MD5Hash(string text)
        {
            MD5 md5 = MD5.Create(); //new MD5CryptoServiceProvider();

            //compute hash from the bytes of text  
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

            //get hash result after compute it  
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits  
                //for each byte  
                strBuilder.Append(result[i].ToString("X2"));
            }

            return strBuilder.ToString();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

            //UXManager.ColorScheme = "CaranuLightSkinFragment";

            UXManager.Initialize(StandardSkinNames.System);
            //UXManager.ToggleAmbientSkin("Custom Resize Borders", true);

            //UXManager.ToggleAmbientSkin("Custom Resize Borders", true);

            //SkinManager.Instance.AddSkin(new CaranuDarkSkinFragment());
            //SkinManager.Instance.AddSkin(new CaranuLightSkinFragment());
            //UXManager.ColorScheme = StandardSkinNames.System;
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            UXManager.Initialize(StandardSkinNames.Caranu);
            //UXManager.ToggleAmbientSkin("Custom Resize Borders", true);


        }
    }
}
